/*
 * @Author: fishroom
 * @Date: 2023-01-05 15:07:33
 * @LastEditTime: 2023-01-09 14:47:31
 * @FilePath: \koa\app\server\socketServer\socketUserServer.ts
 */
import socket_user from "../../model/socket_user";
import ChitchatUser from "../../socketHandler/ChitchatUser";

class socketUserServer {
  async getUser() {
    return await socket_user.findAll();
  }
  async updataUserById(AUser: any) {
    console.log("修改的值",{
      ...AUser,
      group:
        typeof AUser.group == "string"
          ? AUser.group
          : JSON.stringify(Object.fromEntries(AUser.group)),
      friend:
        typeof AUser.friend == "string"
          ? AUser.friend
          : JSON.stringify(Object.fromEntries(AUser.friend)),
      user_id: undefined,
    });
    
    try {
      await socket_user.update(
        {
          ...AUser,
          group:
            typeof AUser.group == "string"
              ? AUser.group
              : JSON.stringify(Object.fromEntries(AUser.group)),
          friend:
            typeof AUser.friend == "string"
              ? AUser.friend
              : JSON.stringify(Object.fromEntries(AUser.friend)),
          user_id: undefined,
        },
        {
          where: {
            user_id: AUser.user_id,
          },
        }
      );
      return true;
    } catch (error) {
      return error;
    }
  }
  async getUserById(user_id: number) {
    try {
      let admin = await socket_user.findOne({
        where: {
          user_id,
        },
      });
      return admin;
    } catch (error) {
      return error;
    }
  }
  async addSocketUser(newUser: any) {
    try {
      let data = await socket_user.create(newUser);
      return true;
    } catch (error) {
      return error;
    }
  }
}
export default new socketUserServer();
